package com.lzp.customview.load

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.annotation.ColorInt
import com.utils.SizeUtils

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2021/8/4
 */
class CircleLoadingView : View {

    private var progressColor = (0xffff0000).toInt()
    private var progressStrokeWidth = SizeUtils.dip2px(context, 5f)

    private val mProgressRectF = RectF()

    private var isAnimation = false

    private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = progressStrokeWidth
        strokeCap = Paint.Cap.ROUND
        color = progressColor
    }

    private var animationStartProgress = 0f
    private var animationEndProgress = 0f

    private val animation = AnimatorSet().apply {
        val aDuration = 1000L
        val valueAnimator3 = ValueAnimator.ofInt(0, 6)
        valueAnimator3.addUpdateListener { animation ->
            val process = animation.animatedValue as Int
            animationStartProgress = process.toFloat()
            invalidate()
        }
        valueAnimator3.duration = 100
        valueAnimator3.repeatCount = 0

        val valueAnimator1 = ValueAnimator.ofInt(-90, 270)
        valueAnimator1.addUpdateListener { animation ->
            val process = animation.animatedValue as Int
            animationEndProgress = process.toFloat()
        }
        valueAnimator1.duration = aDuration
        valueAnimator1.repeatMode = ValueAnimator.RESTART
        valueAnimator1.repeatCount = ValueAnimator.INFINITE
        valueAnimator1.startDelay = 110

        val valueAnimator2 = ValueAnimator.ofInt(6, 225)
        valueAnimator2.addUpdateListener { animation ->
            val process = animation.animatedValue as Int
            animationStartProgress = process.toFloat()
            invalidate()
        }
        valueAnimator2.duration = aDuration
        valueAnimator2.repeatMode = ValueAnimator.REVERSE
        valueAnimator2.repeatCount = ValueAnimator.INFINITE
        valueAnimator2.startDelay = 110
        play(valueAnimator1).with(valueAnimator2).with(valueAnimator3)
    }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val margin = progressStrokeWidth / 2 + 1
        mProgressRectF.set(margin, margin, (w - margin).toFloat(), (h - margin).toFloat())
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawArc(mProgressRectF, animationEndProgress, animationStartProgress, false, circlePaint)
    }

    private fun startAnimation() {
        if (isAnimation) {
            return
        }
        isAnimation = true
        animation.start()
    }

    private fun cancelAnimation() {
        isAnimation = false
        animation.cancel()
    }

    override fun onDetachedFromWindow() {
        cancelAnimation()
        super.onDetachedFromWindow()
    }

    override fun onAttachedToWindow() {
        startAnimation()
        super.onAttachedToWindow()
    }

    fun setProgressColor(@ColorInt color:Int){
        this.progressColor = color
        circlePaint.color = color
        invalidate()
    }

    fun setProgressStrokeWidth(strokeWidth:Float){
        this.progressStrokeWidth = strokeWidth
        circlePaint.strokeWidth = strokeWidth
        invalidate()
    }
}